﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BusinessLogic
{
    public class NewsLogic
    {
        public List<dynamic> getPageDate<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int Total)
                  where T : class
        {
            Model1Container db = new Model1Container();
            Total = db.Set<T>().Where(where).Count();
            var list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }
        public tb_News GetForm(string keyValue)
        {
            Model1Container db = new Model1Container();
            return db.tb_News.Find(keyValue);
        }
        public string AddInfo(tb_News model)
        {
            string result = "";
            try
            {
                Model1Container db = new Model1Container();
                db.tb_News.Add(model);
                db.SaveChanges();
                result = model.ID;
            }
            catch (Exception ex)
            {
                return "";
            }
            return result;
        }

        public int UpdateInfo(tb_News model)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var newModel = db.tb_News.Where(c => c.ID == model.ID).First();
                    newModel.Title = model.Title;
                    newModel.Pic = model.Pic;
                    newModel.Description = model.Description;
                    newModel.Details = model.Details;
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
    }
}
